package cn.weiron.fun.sample.service.impl;


import cn.hutool.core.util.StrUtil;
import cn.weiron.fun.infrastructure.dto.PageResult;
import cn.weiron.fun.infrastructure.utils.PageFactory;
import cn.weiron.fun.sample.dto.SamplePointTimeRequestDto;
import cn.weiron.fun.sample.entity.SamplePointTime;
import cn.weiron.fun.sample.mapper.SamplePointTimeMapper;
import cn.weiron.fun.sample.service.SamplePointTimeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (SamplePointTime)表服务实现类
 *
 * @author makejava
 * @since 2022-07-16 10:36:20
 */
@Service("samplePointTimeServiceImpl")
public class SamplePointTimeServiceImpl  extends ServiceImpl<SamplePointTimeMapper, SamplePointTime> implements SamplePointTimeService {
    @Autowired
    private SamplePointTimeMapper samplePointTimeDao;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public PageResult<SamplePointTime> queryByPage(SamplePointTimeRequestDto dto) {
        LambdaQueryWrapper<SamplePointTime> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(dto.getSearchKey())) {
            wrapper.and(item->item.like(SamplePointTime::getDays,dto.getSearchKey()))
            ;
        }
        wrapper.orderByDesc(SamplePointTime::getId);

        Page<SamplePointTime> objectPage = PageFactory.defaultPage();
        objectPage.setCurrent(dto.getIndex());
        objectPage.setSize(dto.getSize());
        Page<SamplePointTime> page = this.page(objectPage, wrapper);
        List<SamplePointTime> collect = page.getRecords().
                stream().collect(Collectors.toList());
        int total = (int) page.getTotal();

        return new PageResult<>(collect, total);
    }

    /**
     * 新增数据
     *
     * @param samplePointTime 实例对象
     * @return 实例对象
     */
    @Override
    public SamplePointTime insert(SamplePointTime samplePointTime) {
        this.samplePointTimeDao.insert(samplePointTime);
        return samplePointTime;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.samplePointTimeDao.deleteById(id) > 0;
    }

    @Override
    public List<SamplePointTime> queryByUniqueId(String item) {
        LambdaQueryWrapper<SamplePointTime> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SamplePointTime::getSampleUniqueId,item);
        return list(wrapper);
    }
}
